<template>
  <div class="article-container">
    <!-- 导航栏 -->
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      title="文章详情"
    ></van-nav-bar>
    <!-- /导航栏 -->

    <!-- 加载中 loading -->
    <van-loading class="article-loading" v-show="loading" />
    <!-- /加载中 loading -->
    <div class="error" v-if="is404 && !loading">404文章不见了</div>
    <!-- 文章详情 -->
    <div class="detail" v-if="!is404 && !loading">
      <h3 class="title">{{ article.title }}</h3>
      <div class="author">
        <van-image
          round
          width="1rem"
          height="1rem"
          fit="fill"
          :src="article.aut_photo"
        />
        <div class="text">
          <p class="name">{{ article.aut_name }}</p>
          <p class="time">{{ article.pubdate | relativeTime }}</p>
        </div>
        <van-button round size="small" type="info" @click="isFollowed">{{
          article.is_followed ? "取消" : " +关注"
        }}</van-button>
      </div>
      <div class="content">
        <div v-html="article.content"></div>
        <!-- <p>323232</="p> -->
      </div>
      <van-divider>END</van-divider>
      <div class="zan">
        <van-button
          round
          size="small"
          hairline
          type="primary"
          plain
          @click="switchLike"
          :icon="article.attitude === 1 ? 'good-job' : 'good-job-o'"
          >{{ article.attitude === 1 ? "已赞" : "点赞" }}</van-button
        >
        &nbsp;&nbsp;&nbsp;&nbsp;
        <van-button
          round
          size="small"
          hairline
          type="danger"
          @click="switchLove"
          plain
          icon="delete"
          >{{ article.attitude === 0 ? "取消不喜欢" : "不喜欢" }}</van-button
        >
      </div>
    </div>
    <!-- /文章详情 -->
    <ArtComment :articleId="$route.params.articleId" />
  </div>
</template>

<script>
import {
  getArticle,
  deleteFollowing,
  postFollow,
  likeArticle,
  desLikeArticle,
  AddDisloveArticle,
  CancelLoveArticle
} from "@/api/article";
import ArtComment from "./comment.vue";
export default {
  name: "ArticleIndex",
  data() {
    return {
      loading: true, // 控制加载中的 loading 状态
      article: {},
      is404: false
    };
  },
  created() {
    this.LoadArt();
  },
  components: {
    ArtComment
  },
  methods: {
    async LoadArt() {
      const artID = this.$route.params.articleId;

      try {
        const res = await getArticle(artID);
        this.loading = false;
        if (res && res.data) {
          this.article = res.data.data;
        }
      } catch (err) {
        console.dir(err);
        this.loading = false;
        if (err && err.response && err.response.status === 404) {
          this.is404 = true;
        }
      }
    },
    // 用户关注
    async isFollowed() {
      const aut_id = this.article.aut_id;
      if (this.article.is_followed) {
        const res = await deleteFollowing(aut_id);
        console.log(res);
        // 异常情况
      } else {
        const res = await postFollow(aut_id);
        console.log(res);
      }

      this.article.is_followed = !this.article.is_followed;
    },
    // 用户点赞
    async switchLike() {
      // 需要进行一个 -1 无态度 和 1 点赞直接的数据状态切换
      const art_id = this.article.art_id.toString();
      if (this.article.attitude === 1) {
        // 取消点赞

        await desLikeArticle(art_id);
        this.article.attitude = -1;
      } else {
        // 点赞

        await likeArticle(art_id);
        this.article.attitude = 1;
      }
    },
    async switchLove() {
      // 需要进行一个 -1 无态度 和 1 点赞直接的数据状态切换
      const art_id = this.article.art_id.toString();
      if (this.article.attitude === 0) {
        // 取消不喜欢
        await CancelLoveArticle(art_id);

        this.article.attitude = -1;
      } else {
        // 不喜欢
        await AddDisloveArticle(art_id);
        this.article.attitude = 0;
      }
    }
  }
};
</script>

<style scoped lang="less">
.article-container {
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
}
.article-loading {
  padding-top: 100px;
  text-align: center;
}
.error {
  padding-top: 100px;
  text-align: center;
}
.detail {
  padding: 50px 10px;
  .title {
    font-size: 16px;
  }
  .zan {
    text-align: center;
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.3;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    font-size: 14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    /deep/ img {
      max-width: 100%;
      background: #f9f9f9;
    }
  }
}
</style>
